﻿using System.Linq.Expressions;
using Common.DataModels;
using Microsoft.EntityFrameworkCore;

namespace BaseRepository.Db;

public interface IBaseRepository<TEntity> where TEntity : AggregateRoot
{
    IQueryable<TEntity> Qyery { get; }
    DbContext Context { get; }
    /// <summary>
    /// 根据条件查询单个实体
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <param name="isTracking"></param>
    /// <returns></returns>
    Task<TEntity?> GetEntityAsync(Expression<Func<TEntity, bool>> whereExpression, bool isTracking = false);
    /// <summary>
    /// 根据条件查询实体
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <param name="isTracking"></param>
    /// <returns></returns>
    Task<List<TEntity>> GetEntitiesAsync(Expression<Func<TEntity, bool>> whereExpression, bool isTracking = false);
    /// <summary>
    /// 根据条件查询多个实体
    /// </summary>
    /// <typeparam name="TKey"></typeparam>
    /// <param name="whereExpression"></param>
    /// <param name="orderStr"></param>
    /// <param name="isAsc"></param>
    /// <param name="isTracking"></param>
    /// <returns></returns>
    Task<List<TEntity>> GetEntitiesByOrderAsync(Expression<Func<TEntity, bool>> whereExpression, string orderStr = "", bool isTracking = false);
    /// <summary>
    /// 根据条件分页查询和排序
    /// </summary>
    /// <typeparam name="TKey"></typeparam>
    /// <param name="whereExpression"></param>
    /// <param name="orderStr"></param>
    /// <param name="pageIndex"></param>
    /// <param name="pageSize"></param>
    /// <param name="isAsc"></param>
    /// <param name="isTracking"></param>
    /// <returns></returns>
    Task<Tuple<List<TEntity>, int>> GetEntitiesByPageAsync(Expression<Func<TEntity, bool>> whereExpression, string? orderStr = "", int pageIndex = 1, int pageSize = 20, bool isTracking = false);
    /// <summary>
    /// 根据条件分页查询
    /// </summary>
    /// <typeparam name="TKey"></typeparam>
    /// <param name="whereExpression"></param>
    /// <param name="pageIndex"></param>
    /// <param name="pageSize"></param>
    /// <param name="isTracking"></param>
    /// <returns></returns>
    Task<Tuple<List<TEntity>, int>> GetEntitiesByPageAsync(Expression<Func<TEntity, bool>> whereExpression, int pageIndex = 1, int pageSize = 20, bool isTracking = false);
    /// <summary>
    /// 添加实体
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    Task<int> CreateAsync(TEntity entity);
    /// <summary>
    /// 添加实体
    /// </summary>
    /// <param name="entities"></param>
    /// <returns></returns>
    Task<int> CreateAsync(IEnumerable<TEntity> entities);

    ///// <summary>
    ///// 高效批量插入
    ///// </summary>
    ///// <param name="entities"></param>
    ///// <returns></returns>
    //public Task BulkInsert(IEnumerable<TEntity> entities);
    /// <summary>
    /// 更新实体
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    Task<int> UpdateAsync(TEntity entity);
    /// <summary>
    /// 更新实体
    /// </summary>
    /// <param name="entities"></param>
    /// <returns></returns>
    Task<int> UpdateAsync(IEnumerable<TEntity> entities);
    /// <summary>
    /// 删除实体
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    Task<int> DeleteAsync(long id);
    /// <summary>
    /// 删除实体
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    Task<int> DeleteAsync(IEnumerable<long> ids);
    /// <summary>
    /// 假删
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    //Task<int> FalseDeletionAsync(IEnumerable<long> ids);


}